<?php

namespace app\common\model;

use app\common\Code;
use app\common\Session;
use think\Model;

class Trade extends Model
{
    //交易类型
    const TRADE_RECHARGE = 201;  #充值
    const TRADE_SHOP = 202;      #购物
    const TRADE_FANLI = 203;     #返利
    const TRADE_TIXIAN = 204;    #提现
    const TRADE_SAOMA = 205;     #扫条形码
    const TRADE_FULI = 206;      #福利
    const TRADE_CUSTORMER = 207;     #售后返现
//    const TRADE_HXB = 208;     #华夏银行
//    const TRADE_CANCEL_ORDER = 2009;     #取消订单
    const TRADE_SYSTEM_FUWU_FEE = 2010;     #系统服务费
//    const TRADE_SHOP_YUN_FEE = 2011;     #运费
    const TRADE_SHOP_YUE = 2012;     #余额购物代付款
    const TRADE_BAO_ZHENG_JIN = 2013; ##保证金
    const TRADE_BOND_CANCEL = 2014;     #保证金退回
    const TRADE_ZHUAN_ZHANG = 2016;     #转账

    //充值类型
    const PAYMENT_ALIPAY = 301;      #支付宝
    const PAYMENT_WEIXIN_PC = 302;   #微信pc
    const PAYMENT_WEIXIN_APP = 303;  #微信app
    const PAYMENT_WEIXIN_HAO = 304;  #微信号
    const PAYMENT_YUE = 305;         #余额
    const PAYMENT_TIXIAN = 306;      #提现
    const PAYMENT_UNION = 307;       #银联

    const FEE_DEDUCTION_RATIO = 0.006;   #提现扣费比率

    /**
     * 由trade_type得到类型名称
     * @param $trade_type
     * @return string
     */
    public static function getTradeTypeName($trade_type)
    {
        switch ($trade_type) {
            case self::TRADE_RECHARGE:
                return "充值";
            case self::TRADE_SHOP:
                return "购物";
            case self::TRADE_FANLI:
                return "返利";
            case self::TRADE_TIXIAN:
                return "提现";
            case self::TRADE_SAOMA:
                return "扫条形码";
            case self::TRADE_FULI:
                return "福利";
//            case self::TRADE_CANCEL_ORDER:
//                return "取消订单";
            case self::TRADE_SYSTEM_FUWU_FEE:
                return "系统服务费";
//            case self::TRADE_SHOP_YUN_FEE:
//                return "运费";
            case self::TRADE_SHOP_YUE:
                return "余额购物代付款";
            case self::TRADE_ZHUAN_ZHANG:
                return "转账";
            case self::TRADE_BOND_CANCEL:
                return "退回保证金";
            case self::TRADE_BAO_ZHENG_JIN:
                return "保证金";
            default:
                return "未知";
        }
    }

    /**
     * 根据payment_type获取支付名称
     * @param $payment_type
     * @return string
     */
    public static function getPaymentTypeName($payment_type)
    {
        switch ($payment_type) {
            case self::PAYMENT_ALIPAY:
                return "支付宝";
            case self::PAYMENT_WEIXIN_PC:
                return "微信PC";
            case self::PAYMENT_WEIXIN_APP:
                return "微信App";
            case self::PAYMENT_WEIXIN_HAO:
                return "微信号";
            case self::PAYMENT_YUE:
                return "余额";
            case self::PAYMENT_TIXIAN:
                return "提现";
            case self::PAYMENT_UNION:
                return "银联";
            default:
                return "未知";
        }
    }

    /**
     * 交易账单
     * @param $code
     * @param $value
     * @param string $memo
     * @return Trade|null|static
     */
    static function create_shop($code, $value, $memo = '')
    {
        if ($value > 0) {
            $value = 0 - $value;
        }
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $t_o = self::get(["code" => $code]);
        if (empty($t_o)) {
            $t_o = new self();
            $t_o->trade_type = self::TRADE_SHOP;
            $t_o->user_id = $user->id;
            $t_o->code = $code;
            $t_o->value = $value;
            $t_o->is_pay = 0;
            $t_o->memo = $memo;
            $t_o->from = "余额";
            $t_o->to = "平台";
            $t_o->created_time = date("Y-m-d H:i:s");
            $t_o->save();
        }
        return $t_o;
    }

    /**
     * 充值账单
     * @param $code
     * @param $value
     * @param string $memo
     * @return Trade|null|static
     */
    static function create_recharge($code, $value, $memo = '')
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $t_o = self::get(["code" => $code]);
        if (empty($t_o)) {
            $t_o = new self();
            $t_o->trade_type = self::TRADE_RECHARGE;
            $t_o->user_id = $user->id;
            $t_o->code = $code;
            $t_o->value = $value;
            $t_o->is_pay = 0;
            $t_o->memo = $memo;
            $t_o->from = "平台";
            $t_o->to = "余额";
            $t_o->created_time = date("Y-m-d H:i:s");
            $t_o->save();
        }
        return $t_o;
    }

    /**
     * 取消订单返回余额
     * @param $code
     * @param $value
     * @param string $memo
     * @return Trade|null|static
     */
    static function create_cancel_order($code, $value, $memo = '')
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $t_o = self::get(["code" => $code]);
        if (empty($t_o)) {
            $t_o = new self();
            $t_o->trade_type = self::TRADE_RECHARGE;
            $t_o->user_id = $user->id;
            $t_o->code = $code;
            $t_o->value = $value;
            $t_o->is_pay = 1;
            $t_o->memo = $memo;
            $t_o->from = "平台";
            $t_o->to = "余额";
            $t_o->created_time = date("Y-m-d H:i:s");
            $t_o->save();
        }
        return $t_o;
    }


    /**
     * 创建售后账单
     * @param Users $user
     * @param $value
     * @param string $memo
     * @return Trade|null|static
     */
    static function customer_service_change(Users $user, $value, $memo = '')
    {
        $code = sprintf(
            "%s%s%s",
            "customerservice",
            time(),
            rand(1000, 9999)
        );
        $fund = $user->getUserFund();
        $fund->setInc("value", $value);
        $t_o = self::get(["code" => $code]);
        if (empty($t_o)) {
            $t_o = new self();
            $t_o->trade_type = self::TRADE_CUSTORMER;
            $t_o->payment_type = self::PAYMENT_YUE;
            $t_o->user_id = $user->id;
            $t_o->code = $code;
            $t_o->value = $value;
            $t_o->is_pay = 1;
            $t_o->memo = $memo;
            $t_o->from = "平台";
            $t_o->to = "余额";
            $t_o->created_time = date("Y-m-d H:i:s");
            $t_o->save();
        }
        return $t_o;
    }

    /**
     * 创建返利账单
     * @param Users $user
     * @param Order $order
     * @param string $memo
     * @return Trade|null|static
     */
    static function create_rebates(Users $user, Order $order, $memo = '')
    {
        $code = sprintf(
            "%s%s%s",
            "rebates",
            time(),
            rand(1000, 9999)
        );
        $fund = $user->getUserFund();
        $value = abs($order->total);
        $fund->setInc("value", $value);
        $t_o = new self();
        $t_o->payment_type = self::PAYMENT_YUE;
        $t_o->trade_type = self::TRADE_FANLI;
        $t_o->user_id = $user->id;
        $t_o->code = $code;
        $t_o->value = $value;
        $t_o->is_pay = 1;
        $t_o->memo = $memo;
        $t_o->from = "平台";
        $t_o->to = "余额";
        $t_o->created_time = date("Y-m-d H:i:s");
        $t_o->save();
        return $t_o;
    }

    /**
     * 记录交易
     * @param $user
     * @param $code
     * @param $value
     * @param string $memo
     * @return Trade|null|static
     */
    static function create_alipay_tixian_success($user, $code, $value, $memo = '')
    {
        $t_o = self::get(["code" => $code]);
        if (empty($t_o)) {
            $t_o = new self();
            $t_o->trade_type = self::TRADE_TIXIAN;
            $t_o->payment_type = self::PAYMENT_TIXIAN;
            $t_o->user_id = $user->id;
            $t_o->code = $code;
            $t_o->value = $value;
            $t_o->is_pay = 1;
            $t_o->memo = $memo;
            $t_o->from = "余额";
            $t_o->to = "三方账户";
            $t_o->created_time = date("Y-m-d H:i:s");
            $t_o->save();
        }
        return $t_o;
    }

    /**
     * 通过交易码获取交易信息
     * @param $code 交易码
     * @return array|false|\PDOStatement|string|Model
     */
    static function get_trade_by_code($code)
    {
        $s = new self();
        $data = $s->where('code', $code)->find();
        return $data;
    }

    /**
     * 获取支付记录列表(以及此记录的用户信息)
     * @param $param
     * @return \think\Paginator
     */
    static function trade_list($param = [], $cond = [])
    {
        $s = new self();
        #筛选用户
        if (isset($param['user_id']) && !empty($param['user_id'])) {
            $cond['user_id'] = $param['user_id'];
        }
        #筛选条件（交易码或者用户联系方式）
        if (isset($param['search_']) && !empty($param['search_'])) {
            $search_ = $param['search_'];
            $cond['shop_trade.code|shop_users.telephone'] = ["LIKE", "%$search_%"];
        }
        $data = $s->where($cond)
            ->join('shop_users', 'shop_trade.user_id = shop_users.id')
            ->order('shop_trade.id desc')
            ->paginate(30, false, ['query' => request()->param()]);
        return $data;
    }


    /**
     * 找到相关订单
     * @return bool|false|static[]
     */
    function getOrders()
    {
        if (isset($this->code)) {
            $t_o = new TradeOrder();
            $order_ids = $t_o->where(["code" => $this->code])->column("order_id");
            $order_list = Order::all(["order_id" => ["IN", $order_ids]]);
            return $order_list;
        }
        return false;
    }

    /**
     * 支付成功
     * @return bool|false|static[]
     */
    function pay_success($payment_type)
    {
        if (isset($this->code)) {
            $this->payment_type = $payment_type;
            $this->is_pay = 1;
            $this->save();
        }
        return false;
    }


}
